<?php
include_once ("connect.php");
include_once ("log_order.php"); //test!
$action = $_GET['action'];
switch ($action) {
	case 'add' : //新增
		$shipper = htmlspecialchars(stripslashes(trim($_POST['shipper'])));
		$user_id = htmlspecialchars(stripslashes(trim($_POST['user_id'])));
		
		$idc_name1 = htmlspecialchars(stripslashes(trim($_POST['idc_name1'])));
		$country1 = htmlspecialchars(stripslashes(trim($_POST['country1'])));		
		//$province1 = htmlspecialchars(stripslashes(trim($_POST['province1'])));
		$city1 = htmlspecialchars(stripslashes(trim($_POST['city1'])));
		$address1 = htmlspecialchars(stripslashes(trim($_POST['address1'])));
		$postcode1 = htmlspecialchars(stripslashes(trim($_POST['postcode1'])));			
		$tel1 = htmlspecialchars(stripslashes(trim($_POST['tel1'])));
		$email1 = htmlspecialchars(stripslashes(trim($_POST['email1'])));
		$door_num1 = htmlspecialchars(stripslashes(trim($_POST['door_num1'])));
		
		$idc_name2 = htmlspecialchars(stripslashes(trim($_POST['idc_name2'])));
		$country2 = htmlspecialchars(stripslashes(trim($_POST['country2'])));		
		$province2 = htmlspecialchars(stripslashes(trim($_POST['province2'])));
		$city2 = htmlspecialchars(stripslashes(trim($_POST['city2'])));
		$address2 = htmlspecialchars(stripslashes(trim($_POST['address2'])));
		$postcode2 = htmlspecialchars(stripslashes(trim($_POST['postcode2'])));			
		$tel2 = htmlspecialchars(stripslashes(trim($_POST['tel2'])));
		$email2 = htmlspecialchars(stripslashes(trim($_POST['email2'])));
		
		$where = "AND idc_name='$idc_name1' AND country='$country1' AND door_num='$door_num1' AND city='$city1' AND address='$address1' AND postcode='$postcode1' AND tel='$tel1' and email='$email1'";
		$sql = "SELECT id FROM order_address WHERE order_user_id=$user_id AND address_type=1 ".$where;	
		$logger->debug($sql);  //test!
		$result = mysql_query($sql);
		if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$address_id1 = $row['id'];
			
			$sql9 = "UPDATE order_address set valid=1,update_time=now() where id=$address_id1";
			//$logger->debug($sql9);  //test!
			mysql_query($sql9);
		} else {
			$sql1 = "INSERT INTO order_address(order_user_id,address_type,idc_name,country,door_num,city,address,postcode,tel,email,update_time) VALUES($user_id,1,'$idc_name1','$country1','$door_num1','$city1','$address1','$postcode1','$tel1','$email1',now())";
			$logger->debug($sql1);  //test!
			$query1 = mysql_query($sql1);
	
			$logger->debug($sql);  //test!
			$result = mysql_query($sql);
			if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$address_id1 = $row['id'];
			} else {	
				die("操作失败1");
			}
		}
		
		$where = "AND idc_name='$idc_name2' AND country='$country2' AND province='$province2' AND city='$city2' AND address='$address2' AND postcode='$postcode2' AND tel='$tel2' and email='$email2'";
		$sql = "SELECT id FROM order_address WHERE order_user_id=$user_id AND address_type=2 ".$where;	
		$logger->debug($sql);  //test!
		$result = mysql_query($sql);
		if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$address_id2 = $row['id'];
			
			$sql9 = "UPDATE order_address set valid=1,update_time=now() where id=$address_id1";
			//$logger->debug($sql9);  //test!
			mysql_query($sql9);
		} else {
			$sql2 = "INSERT INTO order_address(order_user_id,address_type,idc_name,country,province,city,address,postcode,tel,email,update_time) VALUES($user_id,2,'$idc_name2','$country2','$province2','$city2','$address2','$postcode2','$tel2','$email2',now())";
			$logger->debug($sql2);  //test!
			$query2 = mysql_query($sql2);
	
			$logger->debug($sql);  //test!
			$result = mysql_query($sql);
			if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$address_id2 = $row['id'];
			} else {	
				die("操作失败2");
			}
		}	
		
		$logger->debug("address_id1=".$address_id1." address_id2=".$address_id2);  //test!
		
		$order_id = $_POST['order_id'];
		if(!empty($order_id) && $order_id>0){
			$sql = "UPDATE order_list SET shipper='$shipper',address_from_id=$address_id1,address_to_id=$address_id2,update_time=now() WHERE id=$order_id ";	
			$logger->debug($sql);  //test!
			$rs = mysql_query($sql);			
		}else{			
			$sql3 = "INSERT INTO order_list(order_user_id,shipper,address_from_id,address_to_id,order_status,time_create,update_time) VALUES ($user_id,'$shipper',$address_id1,$address_id2,10,now(),now())";
			$logger->debug($sql3);  //test!
			$query3 = mysql_query($sql3);
			$logger->debug($query3);  //test!

			$sql = "SELECT id FROM order_list WHERE order_user_id=$user_id AND shipper='$shipper' AND address_from_id=$address_id1 AND address_to_id=$address_id2 ORDER BY update_time DESC limit 1";	
			$logger->debug($sql);  //test!
			$result = mysql_query($sql);			
			if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$order_id = $row['id'];
			}	
		}
		
		$logger->debug("order_id=".$order_id);  //test!
		
		echo $order_id;		
		break;	
				
//----------------------------------------------------------------------------------		
	case 'list' : //列表		
		$page =  $_GET['page'];
		$limit = $_GET['rows'];
		$sidx =  $_GET['sidx'];
		$sord =  $_GET['sord'];
		if (!$sidx){
			$sidx = 1;
		}

        $where = '';	
		
		$type_id  = $_GET['ty'];		
        if(!empty($type_id)){
            $where .= " AND address_type=".$type_id ;
		}		
		
		$user_id  = $_GET['uid'];		
        if(!empty($user_id)){
            $where .= " and order_user_id=".$user_id ;
		}

		$sql = "SELECT COUNT(*) AS count FROM order_address WHERE valid=1".$where;
		//$logger->debug($sql);  //test!						
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$count = $row['count'];		
		if ($count > 0) {
			$total_pages = ceil($count / $limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit * $page - $limit;
		if ($start<0) $start = 0;
		$sql = "SELECT * FROM order_address WHERE valid=1".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
		//$logger->debug($sql);  //test!
		$result = mysql_query($sql) or die("Couldn t execute query." . mysql_error());

		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i = 0;
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$responce->rows[$i]['id'] = $row[id];
			$opt = "<a href='order_address.php?id=".$row['id']."'>地址</a>";
			$opt .= "&nbsp;&nbsp;<a href='order_item.php?id=".$row['id']."'>物品</a>";
			$opt .= "&nbsp;&nbsp;<a href='order_detail.php?id=".$row['id']."'>详细</a>";
			
			if($type_id == 1){
				$p1=$row['city'];
				$p2=$row['door_num'];
			}else{
				$p1=$row['province'];
				$p2=$row['city'];
			}
			
			$responce->rows[$i]['cell'] = array (
				$row['id'],
				$row['idc_name'],
				$row['tel'],
				$row['email'],
				$row['country'],				
				$p1,
				$p2,									
				$row['address'],
				$row['postcode'],
				$opt
			);
			$i++;
		}
		echo json_encode($responce);		
		break;		
		
		
		
//----------------------------------------------------------------------------------
		
	case 'del' : //删除
		$ids = $_POST['ids'];		
		//$logger->debug("ids=".$ids);  //test!		
		//$logger->debug("link=".$db_link);  //test!
		
		if (empty ($ids))
			die("0");
		$sql = "UPDATE order_address set valid=0,update_time=now() where id IN($ids)";
		$logger->debug($sql);  //test!
		mysql_query($sql);	
		if (mysql_affected_rows($db_link)) {
			echo $ids;
		} else {
			die("0");
		} 
		
		//delAllSelect($ids, $db_link);
		break;
	case '' :
		echo 'Bad request.';
		break;
}

//批量删除操作
function delAllSelect($ids, $db_link) {
	if (empty ($ids))
		die("0");
	mysql_query("update order_address set valid=0 where id in($ids)");
	if (mysql_affected_rows($db_link)) {
		echo $ids;
	} else {
		die("0");
	}
}


//------------------
//处理接收jqGrid提交查询的中文字符串
function unescape($str) 
{ 
    $ret = ''; 
    $len = strlen($str); 
    for ($i = 0; $i < $len; $i ++) 
    { 
        if ($str[$i] == '%' && $str[$i + 1] == 'u') 
        { 
            $val = hexdec(substr($str, $i + 2, 4)); 
            if ($val < 0x7f) 
                $ret .= chr($val); 
            else  
                if ($val < 0x800) 
                    $ret .= chr(0xc0 | ($val >> 6)) . 
                     chr(0x80 | ($val & 0x3f)); 
                else 
                    $ret .= chr(0xe0 | ($val >> 12)) . 
                     chr(0x80 | (($val >> 6) & 0x3f)) . 
                     chr(0x80 | ($val & 0x3f)); 
            $i += 5; 
        } else  
            if ($str[$i] == '%') 
            { 
                $ret .= urldecode(substr($str, $i, 3)); 
                $i += 2; 
            } else 
                $ret .= $str[$i]; 
    } 
    return $ret; 
}



?>